package bonjin.unitconv;

import android.app.*;
import android.content.*;
import android.content.DialogInterface.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class UnitConvertor extends Activity {
    /** Called when the activity is first created. */
	private TextView tv1, tv2;
	public int curCat = 0, curSunit = 0, curTunit = curSunit + 1;
	public class selUnit {
		public int sourceUnit = 0, targetUnit = sourceUnit + 1;
	}
	public selUnit[] su;
	public String numSource = "0", numTarget = "0";
	private AlertDialog.Builder ad;
	private Button btnCat;
	private String[] cat;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ad = new AlertDialog.Builder(UnitConvertor.this);
        
        cat = getResources().getStringArray(R.array.category);
        for(int i=0;i<cat.length;i++) su[i] = new selUnit();
        
        tv1 = (TextView) findViewById(R.id.txtSource);
        tv2 = (TextView) findViewById(R.id.txtTarget);
        btnCat = (Button) findViewById(R.id.btnCategory);
        btnCat.setText(cat[curCat]);
        
        btnCat.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ad.setTitle(R.string.prompt_category);
				ad.setItems(cat, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						btnCat.setText(cat[which]);
						curCat = which;
						curSunit = su[curCat].sourceUnit;
						curTunit = su[curCat].targetUnit;
					}
				});
				ad.show();
			}
		});
        
        tv1.setOnClickListener(new View.OnClickListener() {
        	private int lid;
        	
			@Override
			public void onClick(View v) {
				String catName = "cat".concat(Integer.toString(curCat));
				lid = getResources().getIdentifier(catName, "array", getPackageName());

				ad.setTitle(R.string.prompt_unit_list);
				ad.setItems(lid, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						showMsg(getResources().getStringArray(lid)[which], Toast.LENGTH_SHORT);
					}
				});
				ad.show();
			}
		});
        
        tv2.setOnClickListener(new View.OnClickListener() {
        	private int lid;
        	
			@Override
			public void onClick(View v) {
				String catName = "cat".concat(Integer.toString(curCat));
				lid = getResources().getIdentifier(catName, "array", getPackageName());

				ad.setTitle(R.string.prompt_unit_list);
				ad.setItems(lid, new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						showMsg(getResources().getStringArray(lid)[which], Toast.LENGTH_SHORT);
					}
				});
				ad.show();
			}
		});
        
        findViewById(R.id.btnNum1).setOnClickListener(btnListener);
        findViewById(R.id.btnNum2).setOnClickListener(btnListener);
        findViewById(R.id.btnNum3).setOnClickListener(btnListener);
        findViewById(R.id.btnNum4).setOnClickListener(btnListener);
        findViewById(R.id.btnNum5).setOnClickListener(btnListener);
        findViewById(R.id.btnNum6).setOnClickListener(btnListener);
        findViewById(R.id.btnNum7).setOnClickListener(btnListener);
        findViewById(R.id.btnNum8).setOnClickListener(btnListener);
        findViewById(R.id.btnNum9).setOnClickListener(btnListener);
        findViewById(R.id.btnNum0).setOnClickListener(btnListener);
        findViewById(R.id.btnNumDot).setOnClickListener(btnListener);
        findViewById(R.id.btnNumDel).setOnClickListener(btnListener);
        findViewById(R.id.btnNumDel).setOnLongClickListener(new Button.OnLongClickListener(){
			@Override
			public boolean onLongClick(View arg0) {
				numSource = "0";
				tv1.setText(numSource.concat(" cm"));
				return true;
			}
        });
    }
    
    Button.OnClickListener btnListener = new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.btnNum1 :
				if(numSource.equals("0")) numSource = "1";
				else numSource = numSource.concat("1");
				break;
			case R.id.btnNum2 :
				if(numSource.equals("0")) numSource = "2";
				else numSource = numSource.concat("2");
				break;
			case R.id.btnNum3 :
				if(numSource.equals("0")) numSource = "3";
				else numSource = numSource.concat("3");
				break;
			case R.id.btnNum4 :
				if(numSource.equals("0")) numSource = "4";
				else numSource = numSource.concat("4");
				break;
			case R.id.btnNum5 :
				if(numSource.equals("0")) numSource = "5";
				else numSource = numSource.concat("5");
				break;
			case R.id.btnNum6 :
				if(numSource.equals("0")) numSource = "6";
				else numSource = numSource.concat("6");
				break;
			case R.id.btnNum7 :
				if(numSource.equals("0")) numSource = "7";
				else numSource = numSource.concat("7");
				break;
			case R.id.btnNum8 :
				if(numSource.equals("0")) numSource = "8";
				else numSource = numSource.concat("8");
				break;
			case R.id.btnNum9 :
				if(numSource.equals("0")) numSource = "9";
				else numSource = numSource.concat("9");
				break;
			case R.id.btnNum0 :
				if(!numSource.equals("0")) numSource = numSource.concat("0");
				break;
			case R.id.btnNumDot :
				if(!numSource.contains(".")) numSource = numSource.concat(".");
				break;
			case R.id.btnNumDel :
				if(numSource.length() > 1){
					numSource = numSource.substring(0, numSource.length()-1);
				}else{
					numSource = "0";
				}
				break;
			}
			tv1.setText(numSource.concat(" cm"));
		}
    	
    };
    
    public void showMsg(CharSequence msg, int option){
    	Toast.makeText(this, msg, option).show();
    }
    
}